package com.tjsinfo.tjsoa.workplan.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanExamine;

public class WorkPlanDefineDao extends HibernateBaseDao implements IWorkPlanDefineDao{
	/**
	 * 查询默认可以查询的上报人用类型

	 * @param 缺省的工作日志

	 * @return
	 */
	public WorkPlanDefine selectDefineAndType(final WorkPlanDefine workPlanDefine)
	{
		return (WorkPlanDefine)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkPlanDefine work where work.userId = ? and work.type = ?");
						query.setParameter(0, workPlanDefine.getTjsUser().getUserId());
						query.setParameter(1, workPlanDefine.getType());
						return query.uniqueResult();
					}
					
				});
	}
	/**
	 * 查询默认人
	 * @param workPlanDefine
	 * @return
	 */
	public List<WorkPlanExamine> selectDefineUserNoType( WorkPlanDefine workPlanDefine)
	{
		List<WorkPlanExamine> list = null;
		Session session = getSession();
		try {
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkPlanDefine work inner join fetch  work.tjsUser where work.userId = ?");
			query.setParameter(0, workPlanDefine.getTjsUser().getUserId());
			list =	query.list();
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}
	/**
	 * 查询默认可以查询的上报人
	 * @param workPlanDefine
	 * @return
	 */
	/*public List<WorkPlanExamine> selectUser(final WorkPlanDefine workPlanDefine)
	{
		return (List<WorkPlanExamine>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkPlanDefine work where work.tjsUserByDefineId.id = ? and work.type = ?");
						query.setParameter(0, workPlanDefine.getTjsUserByDefineId().getUserId());
						query.setParameter(1, workPlanDefine.getType());
						return query.list();
					}
					
				});
		}*/
	/**
	 * 查询默认可以查询的上报人不用类型
	 * @param workPlanDefine
	 * @return
	 */
	public List<WorkPlanExamine> selectUserNoType(final WorkPlanDefine workPlanDefine)
	{
		return (List<WorkPlanExamine>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkPlanDefine work where work.tjsUser.userId = ?");
						query.setParameter(0, workPlanDefine.getTjsUser().getUserId());
						
						return query.list();
					}
					
				});
		
	}
	
	public void addDefintlist(List<WorkPlanDefine> list) {
		// TODO Auto-generated method stub
		for (WorkPlanDefine workPlanDefine : list) {
			save(workPlanDefine);
		}
	}
	/**
	 * 查询可以审批的人
	 * @param 缺省的工作日志

	 * @return
	 */
	public List selectDefineUser( WorkPlanDefine workPlanDefine)
	{
		List<WorkPlanExamine> list = null;
		Session session = getSession();
		try {
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkPlanDefine work  where work.userId = ?");
			query.setParameter(0, workPlanDefine.getTjsUser().getUserId());
			list =	query.list();
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}
	/**
	 * 查询可以查询的提交人
	 * @param workPlanDefine
	 * @return
	 */
	/*public List selectDefineUser( WorkPlanDefine workPlanDefine)
	{
		List<WorkPlanExamine> list = null;
		Session session = getSession();
		try {
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkPlanDefine work  where work.tjsUserByDefineId.id = ?");
			query.setParameter(0, workPlanDefine.getTjsUserByDefineId().getUserId());
			list =	query.list();
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}*/
}
